<?php

namespace App\V3\V3_2_7\Services;

use App\Services\CommonService;
use App\V3\V3_2_7\Models\ShelfLocationTagModel;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\JsonResponse;

class ShelfLocationTagService
{
    /**
     *创建仓库、库位标签
     * Author: zlf
     * Created at 2024/9/5 10:27
     * @param $data
     * @return JsonResponse|null
     */
    public static function add($data, $time): ?JsonResponse
    {
        if (ShelfLocationTagModel::query()->where([
            'tag_name' => $data['tag_name'],
            'delete_flag' => 1
        ])->exists()) {
            return fail('', '标签名称：' . $data['tag_name'] . ' 已存在！');
        }
        $bool = ShelfLocationTagModel::query()->insert($data);
        if ($bool) {
            CommonService::operateResult($data['user_id'], $time, $data, '添加成功');
            return success();
        } else
            return fail('', '添加失败!');
    }

    /**
     *修改货架、库位标签
     * Author: zlf
     * Created at 2024/9/5 11:02
     * @param $data
     * @param $time
     * @return JsonResponse
     */
    public static function upd($data, $time): JsonResponse
    {
        if(trim($data['tag_name']) ==='空闲'){
            return fail('', '空闲标签，不可以修改');
        }
        $model = ShelfLocationTagModel::query()->where([
            'id' => $data['id'],
            'delete_flag' => 0
        ]);
        if ($model->exists()) {
            if (ShelfLocationTagModel::query()->where([
                'tag_name' => $data['tag_name'],
                'delete_flag' => 1,
                ['id', '!=', $data['id']]
            ])->exists()) {
                return fail('', '标签名称：' . $data['tag_name'] . ' 已存在！');
            }
            unset($data['id']);
            $bool = $model->update($data);
            if ($bool) {
                CommonService::operateResult($data['user_id'], $time, $data, '修改成功');
                return success();
            } else
                return fail('', '修改失败');
        }
        return fail('', '没有找到标签信息！');
    }

    /**
     *详情
     * Author: zlf
     * Created at 2024/9/5 14:21
     * @param $data
     * @return Builder|Model|object|null
     */
    public static function info($data)
    {
        return ShelfLocationTagModel::query()
            ->select(
                'id',
                'sort',
                'type',
                'tag_name',
                'color'
            )->where([
                'id' => $data['id'],
                'delete_flag' => 0
            ])->first();
    }

    /**
     *删除
     * Author: zlf
     * Created at 2024/9/5 14:22
     * @param $data
     * @param $time
     * @return JsonResponse
     */
    public static function del($data, $time)
    {
        $model = ShelfLocationTagModel::query()->where([
            'id' => $data['id'],
            'delete_flag' => 0
        ]);
        if ($model->exists()) {
            unset($data['id']);
            $bool = $model->update(['delete_flag' => 1]);
            if ($bool) {
                CommonService::operateResult($data['user_id'], $time, $data, '删除成功');
                return success();
            } else
                return fail('', '删除失败');
        }
        return fail('', '没有找到标签信息！');
    }

    /**
     * 列表
     * Author: zlf
     * Created at 2024/9/5 14:22
     * @param $data
     * @return LengthAwarePaginator
     */
    public static function list($data)
    {
        return ShelfLocationTagModel::query()
            ->select(
                'id',
                'sort',
                'type',
                'tag_name',
                'color'
            )->where([
                'delete_flag' => 0
            ])->orderBy('sort', 'desc')
            ->orderBy('type', 'asc')
            ->orderBy('id', 'desc')
            ->paginate($data['pageSize'], ['*'], '', $data['currentPage']);
    }

    /**
     * 列表
     * Author: zlf
     * Created at 2024/9/5 14:22
     * @param $data
     */
    public static function tagList($data)
    {
        return ShelfLocationTagModel::query()
            ->select(
                'id',
                'tag_name',
            )->where([
                'delete_flag' => 0,
                ['id','!=',8]
            ])->whereIn('type', [1,2])
            ->orderBy('sort', 'desc')
            ->get();
    }
}
